package com.dht.imagepicker.albums

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.chad.library.adapter4.BaseQuickAdapter
import com.chad.library.adapter4.viewholder.QuickViewHolder
import com.dht.imagepicker.ImageInfo
import com.dht.imagepicker.R


class AlbumsAdapter : BaseQuickAdapter<ImageInfo, QuickViewHolder>() {

    private  val TAG = "AlbumsAdapter"

    private var layoutParams: ViewGroup.LayoutParams? = null

    override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
        super.onAttachedToRecyclerView(recyclerView)
        val with = context.resources.displayMetrics.widthPixels / 4
        layoutParams = ViewGroup.LayoutParams(with, with)
    }

    override fun onBindViewHolder(holder: QuickViewHolder, position: Int, item: ImageInfo?) {

        item?.let {
            val imageView = holder.getView<ImageView>(R.id.imageView)

            layoutParams?.let { imageView.layoutParams = layoutParams }

            Glide.with(context).asBitmap().centerCrop().load(item.uri).into(imageView)
        }
    }

    override fun onCreateViewHolder(
        context: Context,
        parent: ViewGroup,
        viewType: Int
    ): QuickViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.item_albums, null, false)
        return QuickViewHolder(view)
    }
}